package com.jiufeng.materialdesigndemo;

import android.support.design.widget.BaseTransientBottomBar;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.SwipeDismissBehavior;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

//协调者布局
public class CoordinatorActivity extends AppCompatActivity implements View.OnClickListener, SwipeDismissBehavior.OnDismissListener {
    private Button mButton;
    private TextView mTv_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coordinator);
        initView();
    }

    private void initView() {
        mButton = findViewById(R.id.btn_coordinator);
        mButton.setOnClickListener(this);
        mTv_text = findViewById(R.id.tv_text);
        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mTv_text.getLayoutParams();
        //layoutParams.setBehavior();
        SwipeDismissBehavior<TextView> behavior = new SwipeDismissBehavior<>();
        behavior.setListener(this);
        layoutParams.setBehavior(behavior);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_coordinator:
                //Snakebar，可以左右滑动删除
                Snackbar.make(view, "Snakebar in CoordinatorL", Snackbar.LENGTH_SHORT)
                        .setAction("exit", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //android:taskAffinity   这个属性是无意间在apidemo中看到的
                                //他的作用就是：关闭你启动的所有的activity(这些activity都有android:taskAffinity=":finishing");
                                ActivityCompat.finishAffinity(CoordinatorActivity.this);
                            }
                        })
                        .show();
                break;
        }
    }

    @Override
    public void onDismiss(View view) {
        view.setVisibility(View.GONE);
        Snackbar.make(view,"找回",Snackbar.LENGTH_SHORT)
                .setAction("back", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mTv_text.setVisibility(View.VISIBLE);
                        ViewCompat.animate(mTv_text).alpha(1).start();
                    }
                }).show();
    }

    @Override
    public void onDragStateChanged(int state) {

    }
}
